package com.cheng.chengrobot.bean;

import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

@Component
public class OkHttpConfig
{

    @Bean
    @Qualifier("proxyOkHttp")
    public OkHttpClient.Builder getOkHttpClientBuilderWithProxy()
    {

        return new OkHttpClient().newBuilder().proxy(
                        new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)))
                .connectTimeout(180, TimeUnit.SECONDS) //将连接超时设置为180秒
                .readTimeout(180, TimeUnit.SECONDS) //将读取超时设置为180秒
                .writeTimeout(180, TimeUnit.SECONDS) //将写入超时设置为180秒
                .callTimeout(240, TimeUnit.SECONDS);
    }
    @Bean
    @Qualifier("okHttp")
    public OkHttpClient.Builder getOkHttpClientBuilderNoProxy()
    {

        return new OkHttpClient().newBuilder().proxy(
                        new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)))
                .connectTimeout(180, TimeUnit.SECONDS) //将连接超时设置为180秒
                .readTimeout(180, TimeUnit.SECONDS) //将读取超时设置为180秒
                .writeTimeout(180, TimeUnit.SECONDS) //将写入超时设置为180秒
                .callTimeout(240, TimeUnit.SECONDS);
    }
}
